<?php

    /**
     * @package      Modules
     * @subpackage   Base_Classes_IO
     *
     * @license      GNU Lesser General Public Licence see LICENCE-LGPL file or http://www.gnu.org/licenses/lgpl.html
     */
    abstract class Base_Classes_IO_FileUploader
    {

    ////////////////////////////////////////////////////////////////////////////
    //                                 Variables                              //
    ////////////////////////////////////////////////////////////////////////////

        /**
         * Available extensions
         * 
         * @var     array
         */
        private static $_extensions = array('jpeg', 'jpg', 'gif', 'png');

    ////////////////////////////////////////////////////////////////////////////
    //                                 Methods                                //
    ////////////////////////////////////////////////////////////////////////////

        /**
         * Prepares a file upload
         *
         * @throws      Core_Exception
         * @param       string      $fieldName
         * @param       array       $file
         * @param       array       $path
         * @param       array       $extensions
         * @return      array
         */
        public static function prepare($fieldName, array $file, $path = null, array $extensions = null)
        {
            if ($path === null) {
                $path = self::defaultPath($fieldName);
            }

            if ($extensions === null) {
                $extensions = self::$_extensions;
            }

            if ($file["size"] <= 0 || !is_uploaded_file($file["tmp_name"])) {
                throw new Core_Exception('Fatal error : an error occured while uploading the file');
            }

            $extension = str_replace('.', '', Core_IO_Path::getExtension($file["name"]));

            if (!in_array($extension, $extensions)) {
                throw new Core_Exception('Fatal error : file has not a valid extension');
            }

            return array('path'			=> $path,
                         'file'			=> $file,
                         'extension'	=> $extension);
        }

        /**
         * Uploads a file in a directory
         *
         * @throws      Core_Exception
         * @param       string      $fileName
         * @param       array       $file
         */
        public static function upload($fileName, array $file)
        {
            $fileName = Core_IO_Path::combine($file['path'], $fileName);

            if (!Core_IO_Directory::exists($file['path'])) {
                // Creates folder if it doesn't exist
                Core_IO_Directory::createDirectory($file['path'], 0744);
            }

            if (Core_IO_File::exists($fileName)) {
                // Deletes file if it already exists
                Core_IO_File::delete($fileName);
            }

            if (!move_uploaded_file($file['file']["tmp_name"], $fileName)) {
                throw new Core_Exception('Fatal error : an error occured while copying the file');
            }

            return $fileName;
        }

        /**
         * Uploads a file from binary in a directory
         *
         * @throws      Core_Exception
         * @param       string      $fileName
         * @param       array       $file
         */
        public static function uploadFromBinary($fileName, $path, $content)
        {
            $fileName = Core_IO_Path::combine($path, $fileName);

            if (!Core_IO_Directory::exists($path)) {
                // Creates folder if it doesn't exist
                Core_IO_Directory::createDirectory($path, 0744);
            }

            if (Core_IO_File::exists($fileName)) {
                // Deletes file if it already exists
                Core_IO_File::delete($fileName);
            }

            Core_IO_File::writeAll($fileName, $content);
        }

        /**
         * Returns default path for upload
         *
         * @param   string  $fieldName
         * @return  string
         */
        public static function defaultPath($fieldName, $module = null)
        {
            if ($module === null) {
                $module = strtolower(Core_Context::get());
            }

            return Core_IO_Path::combine(
                                            PROJECT_MEDIA_PATH,
                                            'modules',
                                            $module,
                                            $fieldName
                                        );
        }
    }